//
//  CGPanTranceationAnimation.m
//  GoldChampion
//
//  Created by Mac on 2018/4/14.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "CGPanTranceationAnimation.h"
#import "GCWokDetailViewController.h"
typedef NS_ENUM(NSUInteger, animationState) {
    animationStateNormal,
    animationStateBeigin,
};
@interface CGPanTranceationAnimation()
@property (nonatomic, strong) GCWokDetailViewController *currentVC;
@property(nonatomic, strong) UIPanGestureRecognizer                     *pan;
@property(nonatomic, assign) CGPoint                        beginPoint;
@property(nonatomic, assign)  animationState                          state;
@end
@implementation CGPanTranceationAnimation
-(void)wireToViewController:(UIViewController *)viewController
{
    self.state = animationStateNormal;
    self.currentVC = (id)viewController;
    [self prepareGestureRecognizerInView:self.currentVC.imageView];
}

- (void)prepareGestureRecognizerInView:(UIView*)view {
    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    [view addGestureRecognizer:gesture];
    self.pan = gesture;

}

-(CGFloat)completionSpeed
{
    return 1 - self.percentComplete;
}

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer locationInView:self.currentVC.view];
    NSLog(@"%@",NSStringFromCGPoint(translation));
    if (!self.beginPoint.x) {
        self.beginPoint = translation;
    }
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            NSLog(@"手势开始");
            break;
        case UIGestureRecognizerStateChanged: {
            NSLog(@"手势改变");
            CGFloat yidong = (translation.y - self.beginPoint.y);
            NSLog(@"移动%f",yidong);
            if (yidong > 50 && self.state == animationStateNormal) {
             self.state = animationStateBeigin;
             [self.currentVC.navigationController popViewControllerAnimated:YES];
            }
            if (yidong && self.state == animationStateBeigin) {
                // 2. Calculate the percentage of guesture
                CGFloat fraction =  yidong / (3*UIScreenHeight);
                NSLog(@"百分比%f",fraction);
                [self updateInteractiveTransition:fraction];
            }

            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"手势结束");
            [self finishInteractiveTransition];
        }
        case UIGestureRecognizerStateCancelled: {
             [self finishInteractiveTransition];
            break;
        }
        default:
            break;
    }
}
@end
